import 'dart:async';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

abstract class BaseTitleStatelessWidget extends StatelessWidget {
  @protected
  String setTitle();

  @protected
  Widget getBody(BuildContext context, StreamController streamController);

  @protected
  Widget build(BuildContext context) {
    final StreamController _streamController = StreamController();

    return Scaffold(
      appBar: AppBar(
        title: Text(setTitle()),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () {
            _finishPage(context, _streamController);
          },
        ),
      ),
      body: WillPopScope(
        onWillPop: () async {
          _finishPage(context, _streamController);
          return true;
        },
        child: getBody(context, _streamController),
      ),
    );
  }

  void _finishPage(BuildContext context, StreamController streamController) {
    if (streamController != null) {
      streamController.close();
    }
    Navigator.of(context).pop();
  }

/*DateTime _lastTime;
  bool _isExit() {
    if (_lastTime == null ||
        DateTime.now().difference(_lastTime) >
            Duration(milliseconds: 2 * 1000)) {
      _lastTime = DateTime.now();
      Fluttertoast.showToast(msg: "再次点击退出应用");
      return false;
    }
    return true;
  }*/
}
